const tree = [
  {
    title: '0-0',
    key: '0-0',
    children: [
      {
        title: '0-0-0',
        key: '0-0-0',
        children: [{ title: '0-0-0-0', key: '0-0-0-0' }]
      },
      {
        title: '0-0-1',
        key: '0-0-1'
      }
    ]
  }
]
/**
 * 生成器（Generator）扁平化树结构
 * @param {*} treeData
 * @returns
 */
const flatTreeByGenerator = treeData => {
  const result = []
  function* traverse(tree, pid = 0) {
    for (const ele of tree) {
      yield { ...ele, pid }
      const children = ele?.children || []
      if (children.length) yield* traverse(children, ele.key) // 递归
    }
  }
  for (let ele of traverse(treeData)) {
    result.push(ele)
  }
  return result
}
const flatArr = flatTreeByGenerator(tree)
console.log(flatArr)
